/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.token.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.opgea.token.dao.CompanyDAO;
import com.opgea.token.dao.LoginDAO;
import com.opgea.token.domain.constants.ApplicationConstants;
import com.opgea.token.domain.entities.Company;
import com.opgea.token.domain.entities.Employee;
import com.opgea.token.domain.entities.Login;
import com.opgea.token.domain.entities.OrganizationDetails;
import com.opgea.token.domain.model.CompanyStatus;
import com.opgea.token.domain.model.CurrentTokenModel;
import com.opgea.token.domain.model.MailModel;
import com.opgea.token.listeners.TokenContextLoaderListener;
import com.opgea.token.service.ApplicationService;
import com.opgea.token.service.CompanyService;
import com.opgea.token.service.EmployeeService;
import com.opgea.token.service.mail.MailService;
import com.opgea.token.web.dto.CompanyDTO;

/**
 * 
 * @author Ramesh
 */
@Service
@Transactional
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    private CompanyDAO companyDAO;

    @Autowired
    private LoginDAO loginDAO;

    @Autowired
    private MailService mailService;

    @Autowired
    private EmployeeService employeeDAO;

    @Autowired
    private ApplicationService applicationService;

    @Override
    public CompanyDTO create(CompanyDTO companyDTO) {
	Company company = new Company();

	company.setId(Calendar.getInstance().getTimeInMillis());
	company.setName(companyDTO.getName());

	OrganizationDetails details = new OrganizationDetails();
	details.setWebsite(companyDTO.getWebsite());
	// When company gets registered the entered email will be considered
	// that email id for both company and the first user.
	details.setEmailId(companyDTO.getEmail());
	company.setDetails(details);

	Employee employee = new Employee();
	employee.setEmailId(companyDTO.getEmail());
	employee.setCompany(company);

	List<Employee> empList = new ArrayList<Employee>();
	empList.add(employee);

	company.setEmployeeList(empList);
	companyDAO.create(company);

	CompanyStatus companyStatus = new CompanyStatus();
	companyStatus.setCompanyId(company.getId());
	companyStatus.setName(company.getName());
	applicationService.addCompanyStatus(companyStatus);

	Login login = new Login();
	login.setLoginId(companyDTO.getEmail());
	login.setPassword(ApplicationConstants.DEFAULT_PASSWORD);
	login.setEmployee(employee);
	loginDAO.create(login);

	String[] filePath = { ApplicationConstants.LOGO_PATH };
	// "C:\\attachments\\Guideline.docx"

	MailModel mailModel = new MailModel();
	mailModel.setTo(companyDTO.getEmail());
	mailModel.setFrom("shekharkumargupta@gmail.com");
	mailModel.setSubject("OPGEA Systems");
	mailModel.setBodyText(ApplicationConstants.getLoginMailContnet(
		login.getLoginId(), login.getPassword()));
	mailModel.setFilePath(filePath);

	// mailService.sendMimeMail(mailModel);

	/**
	 * Adding companies' Current Token Context to the
	 * TokenContextLoaderListener
	 */
	CurrentTokenModel currentToken = new CurrentTokenModel();
	currentToken.setCompanyId(company.getId());
	currentToken.setCurrentTokenNumber(0);
	currentToken.setToday(new Date());

	TokenContextLoaderListener.currentTokenMap.put(
		currentToken.getCompanyId(), currentToken);
	return companyDTO;
    }

    @Override
    public CompanyDTO update(CompanyDTO companyDTO) {
	if (companyDTO.getCompanyId() > 0) {
	    Company company = companyDAO.find(companyDTO.getCompanyId());
	    companyDTO.unmarshall(company);
	    companyDAO.update(company);
	} else {
	    create(companyDTO);
	}
	return companyDTO;
    }

    @Override
    public CompanyDTO delete(Long companyId) {
	Company company = companyDAO.find(companyId);
	companyDAO.delete(company);
	CompanyDTO companyDTO = new CompanyDTO();
	companyDTO.marshall(company);
	return companyDTO;
    }

    @Override
    public CompanyDTO find(Long id) {
	Company company = companyDAO.find(id);
	CompanyDTO companyDTO = new CompanyDTO();
	companyDTO.marshall(company);
	return companyDTO;
    }

    @Override
    public List<CompanyDTO> findAll() {
	List<Company> companyList = companyDAO.findAll();
	List<CompanyDTO> companyDTOList = new ArrayList<CompanyDTO>();
	for (Company company : companyList) {
	    CompanyDTO dto = new CompanyDTO();
	    dto.marshall(company);
	    companyDTOList.add(dto);
	}
	return companyDTOList;
    }

}
